﻿namespace KeRui.Common
{
    using iPortal.Segmenter;
    using iPortal.Segmenter.Match;
    using iPortal.Segmenter.Setting;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;

    public class SegmentHelper
    {
        public static string GetKeyword(string str)
        {
            Segment.Init();
            MatchOptions optionsCopy = Settings.Config.GetOptionsCopy();
            MatchParameter parameterCopy = Settings.Config.GetParameterCopy();
            optionsCopy.ChineseNameIdentify = true;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            ICollection<WordInfo> is2 = new Segment().DoSegment(str, optionsCopy, parameterCopy);
            stopwatch.Stop();
            StringBuilder builder = new StringBuilder();
            foreach (WordInfo info in is2)
            {
                if ((info != null) && (info.Word.Length >= 2))
                {
                    builder.AppendFormat("{0} ", info.Word);
                }
            }
            return builder.ToString().Trim();
        }

        public static string GetKeyword(string str, MatchOptions _Options, MatchParameter _Parameters)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            ICollection<WordInfo> is2 = new Segment().DoSegment(str, _Options, _Parameters);
            stopwatch.Stop();
            StringBuilder builder = new StringBuilder();
            foreach (WordInfo info in is2)
            {
                if ((info != null) && (info.Word.Length >= 2))
                {
                    builder.AppendFormat("{0} ", info.Word);
                }
            }
            return builder.ToString().Trim();
        }
    }
}

